/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Realtime.board;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import BUS.Group_userBUS;
import BUS.SubConfig;
import BUS.UserBUS;
import DTO.Group_user;
import DTO.User;
import static java.lang.System.out;
import java.util.List;
import java.sql.Timestamp;
/**
 *
 * @author quangphuc
 */
public class user extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) 
        {
            response.addHeader("Access-Control-Allow-Origin", "*");
            String fn = "-1";
            if(request.getParameter("fn") == null){
                fn = "-1";
            }
            else{
                fn = request.getParameter("fn");
            }
            System.out.println("demo fn : " + fn);
            if(fn.equals("0"))//thuc hien insert du lieu
            {
                User item = new User();
                item.setId(1);
                item.setIsdelete(Boolean.FALSE);
                //item.setCreate_date();
                item.setUser_name(request.getParameter("user_name"));
                item.setPassword(request.getParameter("password"));
                item.setEmail(request.getParameter("email"));
                item.setPhone(request.getParameter("phone"));
                item.setType_id(1);
                long id = this.Insert(item);
                String json = "" + id;
                out.println(json);
            }
            else if(fn.equals("1"))// thuc hien update
            {
                String strId = request.getParameter("id");
                long id = 0;
                try
                {
                    id = Long.parseLong(strId);
                }
                catch(Exception error)
                {
                    out.println(error.toString());
                }
                User item = new User();                
                item.setId(id);
                item.setIsdelete(Boolean.FALSE);
                item.setCreate_date(Timestamp.valueOf(request.getParameter("create_date")));
                item.setUser_name(request.getParameter("user_name"));
                item.setPassword(request.getParameter("password"));
                item.setEmail(request.getParameter("email"));
                item.setPhone(request.getParameter("phone"));
                item.setType_id(1);
                long rs = this.Update(item);
                String json = "" + rs;
                out.println(json);
            }
            else if(fn.equals("2"))//thuc hien delete
            {
                String strId = request.getParameter("id");
                long id = 0;
                try
                {
                    id = Long.parseLong(strId);
                }
                catch(Exception error)
                {
                    out.println(error.toString());
                }
                User item = new User();                
                item.setId(id);
                long rs = this.Delete(item);
                String json = "" + rs;
                out.println(json);
            }
            else
            {
                out.println(getUser());
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    protected String getUser()
    {      
        UserBUS usBUS = new UserBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        //usBUS.insertOnSubmit(item, config);
        List<User> users = usBUS.getAll(config);
        String json = usBUS.ListToJson("user", users);
        config.close();
        return json;
    }
    
    protected long Insert(User item)
    {
        UserBUS uBUS = new UserBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)uBUS.insertOnSubmit(item, config);
        item.setId(id);
        config.close();
        return id;
    }
    
    protected long Update(User item) 
    {
        UserBUS uBUS = new UserBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)uBUS.updateOnSubmint(item, config);
        item.setId(id);
        return id;
    }
    
    protected long Delete(User item)
    {
        UserBUS uBUS = new UserBUS();
        SubConfig config = SubConfig.getInstance();
        config.init();
        long id =(long)uBUS.deleteOnSubmint(item, config);
        item.setId(id);
        config.close();
        return id;
    }
}
